﻿using SIR.Common;
using SIR.Common.Condition;
using SIR.Entities.Interfaces;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SIR.Entities.Implementations
{
	/// <summary>
	/// Data associated to the image analysis process
	/// </summary>
	public class ImageAnalysis : Entity, IImageAnalysis
	{
		private IReadOnlyCollection<IImagePattern> _patterns;
		private IReadOnlyCollection<IColorSpectrum> _spectrums;

		/// <summary>
		/// Gets or sets the identifier for the image
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long ImageId { get; set; }
		/// <summary>
		/// Gets or sets the shortest average distance for all color spectrums
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public double ShortestAverageDistance { get; set; }
		/// <summary>
		/// Gets or sets the color spectrum definition Id associated to the shortest average distance
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long ColorSpectrumDefinitionId { get; set; }
		/// <summary>
		/// Gets or sets the average hue for the image
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public double AverageHue { get; set; }
		/// <summary>
		/// Gets or sets the standard hue deviation for the image
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public double StandardHueDeviation { get; set; }
		/// <summary>
		/// Gets or sets the number of seconds that it took to analyze the image
		/// </summary>
		[DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public double AnalysisDuration { get; set; }
		/// <summary>
		/// Gets or sets a list of the patterns associated to the image
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public IReadOnlyCollection<IImagePattern> Patterns
		{
			get
			{
				if (_patterns == null)
				{
					_patterns = new ReadOnlyCollection<ImagePattern>(DAL.DataServer.Find<ImagePattern>(
						new Predicate("ImageAnalysisId", Id, PredicateOperator.EqualTo)).Results);
				}

				return _patterns;
			}
		}
		/// <summary>
		/// Gets a list of the color spectrums associated to the image analysis
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[DatabaseAccess(DatabaseAccessRights.None)]
		public IReadOnlyCollection<IColorSpectrum> Spectrums
		{
			get
			{
				if (_spectrums == null)
				{
					_spectrums = new ReadOnlyCollection<ColorSpectrum>(DAL.DataServer.Find<ColorSpectrum>(
						new Predicate("ImageAnalysisId", Id, PredicateOperator.EqualTo)).Results);
				}

				return _spectrums;
			}
		}
	}
}

